

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>通过 libvirt 使用 Ceph RBD &mdash; Ceph Documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/ceph.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/ceph.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/css/custom.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script>
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/sphinx_highlight.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex/" />
    <link rel="search" title="Search" href="../../search/" />
    <link rel="next" title="Block Devices and Kubernetes" href="../rbd-kubernetes/" />
    <link rel="prev" title="QEMU 与块设备" href="../qemu-rbd/" /> 
</head>

<body class="wy-body-for-nav">

   
  <header class="top-bar">
    <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="../">Ceph 块设备</a></li>
          <li class="breadcrumb-item"><a href="../rbd-integrations/">Ceph 块设备与第三方对接</a></li>
      <li class="breadcrumb-item active">通过 libvirt 使用 Ceph RBD</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../../_sources/rbd/libvirt.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
  </header>
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search"  style="background: #eee" >
          

          
            <a href="../../" class="icon icon-home"> Ceph
          

          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../start/">Ceph 简介</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../install/">安装 Ceph</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../cephadm/">Cephadm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../rados/">Ceph 存储集群</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../cephfs/">Ceph 文件系统</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../">Ceph 块设备</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../rados-rbd-cmds/">基本命令</a></li>
<li class="toctree-l2"><a class="reference internal" href="../rbd-operations/">运维</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../rbd-integrations/">对接</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="../rbd-ko/">内核模块</a></li>
<li class="toctree-l3"><a class="reference internal" href="../qemu-rbd/">QEMU</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">libvirt</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#ceph">配置 Ceph</a></li>
<li class="toctree-l4"><a class="reference internal" href="#id1">准备虚拟机管理器</a></li>
<li class="toctree-l4"><a class="reference internal" href="#id2">新建虚拟机</a></li>
<li class="toctree-l4"><a class="reference internal" href="#vm">配置 VM</a></li>
<li class="toctree-l4"><a class="reference internal" href="#id3">总结</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-kubernetes/">Kubernetes</a></li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-nomad/">Nomad</a></li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-openstack/">OpenStack</a></li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-cloudstack/">CloudStack</a></li>
<li class="toctree-l3"><a class="reference internal" href="../iscsi-overview/">LIO iSCSI Gateway</a></li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-windows/">Windows</a></li>
<li class="toctree-l3"><a class="reference internal" href="../nvmeof-overview/">NVMe-oF 网关</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../man/">手册页</a></li>
<li class="toctree-l2"><a class="reference internal" href="../api/">APIs</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../radosgw/">Ceph 对象网关</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../mgr/">Ceph 管理器守护进程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../mgr/dashboard/">Ceph 仪表盘</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../monitoring/">监控概览</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../api/">API 文档</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../architecture/">体系结构</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../dev/developer_guide/">开发者指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../dev/internals/">Ceph 内幕</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../governance/">项目管理</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../foundation/">Ceph 基金会</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../ceph-volume/">ceph-volume</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../releases/general/">Ceph 版本（总目录）</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../releases/">Ceph 版本（索引）</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../security/">Security</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../hardware-monitoring/">硬件监控</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../glossary/">Ceph 术语</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../jaegertracing/">Tracing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../translation_cn/">中文版翻译资源</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../">Ceph</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
<div id="dev-warning" class="admonition note">
  <p class="first admonition-title">Notice</p>
  <p class="last">This document is for a development version of Ceph.</p>
</div>
  <div id="docubetter" align="right" style="padding: 5px; font-weight: bold;">
    <a href="https://pad.ceph.com/p/Report_Documentation_Bugs">Report a Documentation Bug</a>
  </div>

  
  <section id="libvirt-ceph-rbd">
<h1>通过 libvirt 使用 Ceph RBD<a class="headerlink" href="#libvirt-ceph-rbd" title="Permalink to this heading"></a></h1>
<p id="index-0"><code class="docutils literal notranslate"><span class="pre">libvirt</span></code> 库是管理程序和软件应用间的一个虚拟机抽象层。有了
<code class="docutils literal notranslate"><span class="pre">libvirt</span></code> ，开发者和系统管理员只需要关注这些管理器的一个通用管理框架、通用 API 、和通用 shell 接口（即 <code class="docutils literal notranslate"><span class="pre">virsh</span></code> ）就可以了，像：</p>
<ul class="simple">
<li><p>QEMU/KVM</p></li>
<li><p>XEN</p></li>
<li><p>LXC</p></li>
<li><p>VirtualBox</p></li>
<li><p>等等</p></li>
</ul>
<p>Ceph 块设备支持 QEMU/KVM ，所以你可以在 Ceph 块设备之上运行能与 <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> 交互的软件。下面的堆栈图解释了 <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> 和
QEMU 如何通过 <code class="docutils literal notranslate"><span class="pre">librbd</span></code> 使用 Ceph 块设备。</p>
<p class="ditaa">
<img src="../../_images/ditaa-591df1554fdd6a732d7dfcdd4501d1cde8e403be.png"/>
</p>
<p><code class="docutils literal notranslate"><span class="pre">libvirt</span></code> 常见于为云解决方案提供 Ceph 块设备，
像 OpenStack 、ClouldStack ，它们用 <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> 和 QEMU/KVM 交互、
QEMU/KVM 再与 Ceph 块设备通过 <code class="docutils literal notranslate"><span class="pre">librbd</span></code> 交互。
详情见<a class="reference external" href="../rbd-openstack">块设备与 OpenStack</a> 和<a class="reference external" href="../rbd-cloudstack">块设备与 CloudStack</a> 。
关于如何安装见<a class="reference external" href="../../install">安装</a>。</p>
<p>你也可以通过 <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> 、 <code class="docutils literal notranslate"><span class="pre">virsh</span></code> 和 <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> API 使用
Ceph 块设备，详情见 <a class="reference external" href="http://www.libvirt.org">libvirt 虚拟化 API</a> 。</p>
<p>要创建使用 Ceph 块设备的虚拟机，请看后面几段。
具体应用时，我们用 <code class="docutils literal notranslate"><span class="pre">libvirt-pool</span></code> 作为存储池名、
<code class="docutils literal notranslate"><span class="pre">client.libvirt</span></code> 作为用户名、 <code class="docutils literal notranslate"><span class="pre">new-libvirt-image</span></code>
作为映像名，你可以任意命名，确保在后续过程中用自己的名字替换掉对应名字即可。</p>
<section id="ceph">
<h2>配置 Ceph<a class="headerlink" href="#ceph" title="Permalink to this heading"></a></h2>
<p>要把 Ceph 用于 <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> ，执行下列步骤：</p>
<ol class="arabic">
<li><p><a class="reference external" href="../../rados/operations/pools#create-a-pool">创建一存储池</a>。本例用 <code class="docutils literal notranslate"><span class="pre">libvirt-pool</span></code> 作存储池名，
配备了 128 个归置组。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ceph</span> <span class="n">osd</span> <span class="n">pool</span> <span class="n">create</span> <span class="n">libvirt</span><span class="o">-</span><span class="n">pool</span>
</pre></div>
</div>
<p>验证存储池是否存在。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ceph</span> <span class="n">osd</span> <span class="n">lspools</span>
</pre></div>
</div>
</li>
<li><p>用 <code class="docutils literal notranslate"><span class="pre">rbd</span></code> 工具初始化这个存储池以用于 RBD ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">rbd</span> <span class="n">pool</span> <span class="n">init</span> <span class="o">&lt;</span><span class="n">pool</span><span class="o">-</span><span class="n">name</span><span class="o">&gt;</span>
</pre></div>
</div>
</li>
<li><p><a class="reference external" href="../../rados/operations/user-management#add-a-user">创建一 Ceph 用户</a>（ 0.9.7 版之前的话用 <code class="docutils literal notranslate"><span class="pre">client.admin</span></code>
），本例用 Ceph 用户名 <code class="docutils literal notranslate"><span class="pre">client.libvirt</span></code> 、且权限限制到
<code class="docutils literal notranslate"><span class="pre">libvirt-pool</span></code> 。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ceph</span> <span class="n">auth</span> <span class="n">get</span><span class="o">-</span><span class="ow">or</span><span class="o">-</span><span class="n">create</span> <span class="n">client</span><span class="o">.</span><span class="n">libvirt</span> <span class="n">mon</span> <span class="s1">&#39;profile rbd&#39;</span> <span class="n">osd</span> <span class="s1">&#39;profile rbd pool=libvirt-pool&#39;</span>
</pre></div>
</div>
<p>验证名字是否存在。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ceph</span> <span class="n">auth</span> <span class="n">ls</span>
</pre></div>
</div>
<p><strong>注：</strong> <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> 访问 Ceph 时将用 <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> 作为 ID ，而不是 <code class="docutils literal notranslate"><span class="pre">client.libvirt</span></code> 。关于 ID 和名字不同的详细解释请参考<a class="reference external" href="../../rados/operations/user-management#user">用户管理——用户</a>和<a class="reference external" href="../../rados/operations/user-management#command-line-usage">用户管理——命令行界面</a>。</p>
</li>
<li><p>用 QEMU 在 RBD 存储池中<a class="reference external" href="../qemu-rbd#creating-images-with-qemu">创建一映像</a>。
本例中映像名为 <code class="docutils literal notranslate"><span class="pre">new-libvirt-image</span></code> 、
存储池为 <code class="docutils literal notranslate"><span class="pre">libvirt-pool</span></code> 。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="n">create</span> <span class="o">-</span><span class="n">f</span> <span class="n">rbd</span> <span class="n">rbd</span><span class="p">:</span><span class="n">libvirt</span><span class="o">-</span><span class="n">pool</span><span class="o">/</span><span class="n">new</span><span class="o">-</span><span class="n">libvirt</span><span class="o">-</span><span class="n">image</span> <span class="mi">2</span><span class="n">G</span>
</pre></div>
</div>
<p>验证映像是否存在。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">rbd</span> <span class="o">-</span><span class="n">p</span> <span class="n">libvirt</span><span class="o">-</span><span class="n">pool</span> <span class="n">ls</span>
</pre></div>
</div>
<p><strong>注：</strong>你也可以用 <a class="reference external" href="../rados-rbd-cmds#creating-a-block-device-image">rbd create</a> 创建映像，但我们建议顺便确认下 QEMU 可正常运行。</p>
</li>
</ol>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>另外，如果你想给这个客户端打开调试日志和管理套接字，你可以在 <code class="docutils literal notranslate"><span class="pre">/etc/ceph/ceph.conf</span></code> 里的相应段落加上：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>[client.libvirt]
log file = /var/log/ceph/qemu-guest-$pid.log
admin socket = /var/run/ceph/$cluster-$type.$id.$pid.$cctid.asok
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">client.libvirt</span></code> 段名应该与上面创建的 cephx 用户一致。
如果启用了 SELinux 或 AppArmor ，
它们可能会阻止客户端进程
（ qemu 调用 libvirt ）的某些操作，
比如写入日志、操作映像或管理套接字
（ <code class="docutils literal notranslate"><span class="pre">/var/log/ceph</span></code> 或 <code class="docutils literal notranslate"><span class="pre">/var/run/ceph</span></code> ）。另外，确保 libvirt 和 qemu 用户们有合适的权限访问指定目录。</p>
</div>
</section>
<section id="id1">
<h2>准备虚拟机管理器<a class="headerlink" href="#id1" title="Permalink to this heading"></a></h2>
<p>即使没 VM 管理器你也可以用 <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> ，
但是用 <code class="docutils literal notranslate"><span class="pre">virt-manager</span></code> 创建域更简单。</p>
<ol class="arabic">
<li><p>安装个虚拟机管理器，详情见 <a class="reference external" href="https://help.ubuntu.com/community/KVM/VirtManager">KVM/VirtManager</a> 。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sudo</span> <span class="n">apt</span><span class="o">-</span><span class="n">get</span> <span class="n">install</span> <span class="n">virt</span><span class="o">-</span><span class="n">manager</span>
</pre></div>
</div>
</li>
<li><p>下载一 OS 映像。</p></li>
<li><p>启动虚拟机管理器。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sudo</span> <span class="n">virt</span><span class="o">-</span><span class="n">manager</span>
</pre></div>
</div>
</li>
</ol>
</section>
<section id="id2">
<h2>新建虚拟机<a class="headerlink" href="#id2" title="Permalink to this heading"></a></h2>
<p>要用 <code class="docutils literal notranslate"><span class="pre">virt-manager</span></code> 创建 VM ，按下列步骤：</p>
<ol class="arabic">
<li><p>点击 <strong>Create New Virtual Machine</strong> 按钮。</p></li>
<li><p>为新虚拟机域命名，本例中我们用 <code class="docutils literal notranslate"><span class="pre">libvirt-virtual-machine</span></code> ，你可以任意命名，在后续命令行和配置实例中替换掉
<code class="docutils literal notranslate"><span class="pre">libvirt-virtual-machine</span></code> 即可。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">libvirt</span><span class="o">-</span><span class="n">virtual</span><span class="o">-</span><span class="n">machine</span>
</pre></div>
</div>
</li>
<li><p>导入映像。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">/</span><span class="n">path</span><span class="o">/</span><span class="n">to</span><span class="o">/</span><span class="n">image</span><span class="o">/</span><span class="n">recent</span><span class="o">-</span><span class="n">linux</span><span class="o">.</span><span class="n">img</span>
</pre></div>
</div>
<p><strong>注：</strong>导入一个近期映像，一些较老的映像未必能正确地重扫描虚拟设备。</p>
</li>
<li><p>配置并启动 VM 。</p></li>
<li><p>用 <code class="docutils literal notranslate"><span class="pre">virsh</span> <span class="pre">list</span></code> 验证 VM 域存在。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sudo</span> <span class="n">virsh</span> <span class="nb">list</span>
</pre></div>
</div>
</li>
<li><p>登入 VM （ root/root ）</p></li>
<li><p>改配置让它使用 Ceph 前停止 VM 。</p></li>
</ol>
</section>
<section id="vm">
<h2>配置 VM<a class="headerlink" href="#vm" title="Permalink to this heading"></a></h2>
<p>配置 VM 使用 Ceph 时，切记尽量用 <code class="docutils literal notranslate"><span class="pre">virsh</span></code> 。
另外， <code class="docutils literal notranslate"><span class="pre">virsh</span></code> 命令通常需要 root 权限
（即 <code class="docutils literal notranslate"><span class="pre">sudo</span></code> ），否则不会返回正确结果或提示你需要 root 权限，
<code class="docutils literal notranslate"><span class="pre">virsh</span></code> 命令参考见 <a class="reference external" href="http://www.libvirt.org/virshcmdref.html">Virsh 命令参考</a>。</p>
<ol class="arabic">
<li><p>用 <code class="docutils literal notranslate"><span class="pre">virsh</span> <span class="pre">edit</span></code> 打开配置文件。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sudo</span> <span class="n">virsh</span> <span class="n">edit</span> <span class="p">{</span><span class="n">vm</span><span class="o">-</span><span class="n">domain</span><span class="o">-</span><span class="n">name</span><span class="p">}</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">&lt;devices&gt;</span></code> 下应该有 <code class="docutils literal notranslate"><span class="pre">&lt;disk&gt;</span></code> 条目。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="n">devices</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">emulator</span><span class="o">&gt;/</span><span class="n">usr</span><span class="o">/</span><span class="nb">bin</span><span class="o">/</span><span class="n">kvm</span><span class="o">&lt;/</span><span class="n">emulator</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">disk</span> <span class="nb">type</span><span class="o">=</span><span class="s1">&#39;file&#39;</span> <span class="n">device</span><span class="o">=</span><span class="s1">&#39;disk&#39;</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">driver</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;qemu&#39;</span> <span class="nb">type</span><span class="o">=</span><span class="s1">&#39;raw&#39;</span><span class="o">/&gt;</span>
        <span class="o">&lt;</span><span class="n">source</span> <span class="n">file</span><span class="o">=</span><span class="s1">&#39;/path/to/image/recent-linux.img&#39;</span><span class="o">/&gt;</span>
        <span class="o">&lt;</span><span class="n">target</span> <span class="n">dev</span><span class="o">=</span><span class="s1">&#39;vda&#39;</span> <span class="n">bus</span><span class="o">=</span><span class="s1">&#39;virtio&#39;</span><span class="o">/&gt;</span>
        <span class="o">&lt;</span><span class="n">address</span> <span class="nb">type</span><span class="o">=</span><span class="s1">&#39;drive&#39;</span> <span class="n">controller</span><span class="o">=</span><span class="s1">&#39;0&#39;</span> <span class="n">bus</span><span class="o">=</span><span class="s1">&#39;0&#39;</span> <span class="n">unit</span><span class="o">=</span><span class="s1">&#39;0&#39;</span><span class="o">/&gt;</span>
    <span class="o">&lt;/</span><span class="n">disk</span><span class="o">&gt;</span>
</pre></div>
</div>
<p>用你的 OS 映像路径取代 <code class="docutils literal notranslate"><span class="pre">/path/to/image/recent-linux.img</span></code> ，可利用较快的 <code class="docutils literal notranslate"><span class="pre">virtio</span></code> 总线的最低内核版本是 2.6.25 ，参见
<a class="reference external" href="http://www.linux-kvm.org/page/Virtio">Virtio</a> 。</p>
<p><strong>重要：</strong>要用 <code class="docutils literal notranslate"><span class="pre">sudo</span> <span class="pre">virsh</span> <span class="pre">edit</span></code> 而非文本编辑器，如果你用文本编辑器编辑了 <code class="docutils literal notranslate"><span class="pre">/etc/libvirt/qemu</span></code> 下的配置文件，
<code class="docutils literal notranslate"><span class="pre">libvirt</span></code> 未必能感知你做的更改。如果 <code class="docutils literal notranslate"><span class="pre">/etc/libvirt/qemu</span></code>
下的 XML 文件和 <code class="docutils literal notranslate"><span class="pre">sudo</span> <span class="pre">virsh</span> <span class="pre">dumpxml</span> <span class="pre">{vm-domain-name}</span></code> 输出结果内容不同， VM 可能会运行异常。</p>
</li>
<li><p>把你创建的 Ceph RBD 映像创建为 <code class="docutils literal notranslate"><span class="pre">&lt;disk&gt;</span></code> 条目。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="n">disk</span> <span class="nb">type</span><span class="o">=</span><span class="s1">&#39;network&#39;</span> <span class="n">device</span><span class="o">=</span><span class="s1">&#39;disk&#39;</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">source</span> <span class="n">protocol</span><span class="o">=</span><span class="s1">&#39;rbd&#39;</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;libvirt-pool/new-libvirt-image&#39;</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">host</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;{monitor-host}&#39;</span> <span class="n">port</span><span class="o">=</span><span class="s1">&#39;6789&#39;</span><span class="o">/&gt;</span>
    <span class="o">&lt;/</span><span class="n">source</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">target</span> <span class="n">dev</span><span class="o">=</span><span class="s1">&#39;vdb&#39;</span> <span class="n">bus</span><span class="o">=</span><span class="s1">&#39;virtio&#39;</span><span class="o">/&gt;</span>
<span class="o">&lt;/</span><span class="n">disk</span><span class="o">&gt;</span>
</pre></div>
</div>
<p>用你的主机名替换 <code class="docutils literal notranslate"><span class="pre">{monitor-host}</span></code> 、
有必要的话替换存储池、映像名。
你可以为 Ceph 监视器添加多条 <code class="docutils literal notranslate"><span class="pre">&lt;host&gt;</span></code> ，
<code class="docutils literal notranslate"><span class="pre">dev</span></code> 属性是将出现在 VM 之 <code class="docutils literal notranslate"><span class="pre">/dev</span></code> 目录下的逻辑设备名，
可选的 <code class="docutils literal notranslate"><span class="pre">bus</span></code> 属性是要模拟的磁盘类型。
可用和驱动相关，如 ide 、 scsi 、
virtio 、 xen 、 usb 或 sata 。</p>
<p>关于 <code class="docutils literal notranslate"><span class="pre">&lt;disk&gt;</span></code> 标签及其子标签和属性，详见<a class="reference external" href="http://www.libvirt.org/formatdomain.html#elementsDisks">硬盘</a>。</p>
</li>
<li><p>保存文件。</p></li>
<li><p>如果你的 Ceph 存储集群启用了 <a class="reference external" href="../../rados/configuration/auth-config-ref">Ceph 认证</a>（默认已启用），那么必须生成一个密钥。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">cat</span> <span class="o">&gt;</span> <span class="n">secret</span><span class="o">.</span><span class="n">xml</span> <span class="o">&lt;&lt;</span><span class="n">EOF</span>
<span class="o">&lt;</span><span class="n">secret</span> <span class="n">ephemeral</span><span class="o">=</span><span class="s1">&#39;no&#39;</span> <span class="n">private</span><span class="o">=</span><span class="s1">&#39;no&#39;</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">usage</span> <span class="nb">type</span><span class="o">=</span><span class="s1">&#39;ceph&#39;</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">name</span><span class="o">&gt;</span><span class="n">client</span><span class="o">.</span><span class="n">libvirt</span> <span class="n">secret</span><span class="o">&lt;/</span><span class="n">name</span><span class="o">&gt;</span>
    <span class="o">&lt;/</span><span class="n">usage</span><span class="o">&gt;</span>
<span class="o">&lt;/</span><span class="n">secret</span><span class="o">&gt;</span>
<span class="n">EOF</span>
</pre></div>
</div>
</li>
<li><p>定义密钥。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sudo</span> <span class="n">virsh</span> <span class="n">secret</span><span class="o">-</span><span class="n">define</span> <span class="o">--</span><span class="n">file</span> <span class="n">secret</span><span class="o">.</span><span class="n">xml</span>
<span class="p">{</span><span class="n">uuid</span> <span class="n">of</span> <span class="n">secret</span><span class="p">}</span>
</pre></div>
</div>
</li>
<li><p>获取 <code class="docutils literal notranslate"><span class="pre">client.libvirt</span></code> 密钥并把字符串保存于文件。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ceph</span> <span class="n">auth</span> <span class="n">get</span><span class="o">-</span><span class="n">key</span> <span class="n">client</span><span class="o">.</span><span class="n">libvirt</span> <span class="o">|</span> <span class="n">sudo</span> <span class="n">tee</span> <span class="n">client</span><span class="o">.</span><span class="n">libvirt</span><span class="o">.</span><span class="n">key</span>
</pre></div>
</div>
</li>
<li><p>设置密钥的 UUID 。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>sudo virsh secret-set-value --secret {uuid of secret} --base64 $(cat client.libvirt.key) &amp;&amp; rm client.libvirt.key secret.xml
</pre></div>
</div>
<p>还必须手动设置密钥，把下面的 <code class="docutils literal notranslate"><span class="pre">&lt;auth&gt;</span></code> 条目添加到前面的
<code class="docutils literal notranslate"><span class="pre">&lt;disk&gt;</span></code> 标签内（用上一命令的输出结果替换掉 <code class="docutils literal notranslate"><span class="pre">uuid</span></code>
值）。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sudo</span> <span class="n">virsh</span> <span class="n">edit</span> <span class="p">{</span><span class="n">vm</span><span class="o">-</span><span class="n">domain</span><span class="o">-</span><span class="n">name</span><span class="p">}</span>
</pre></div>
</div>
<p>然后，把 <code class="docutils literal notranslate"><span class="pre">&lt;auth&gt;&lt;/auth&gt;</span></code> 标签加进域配置文件：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">...</span>
<span class="o">&lt;/</span><span class="n">source</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="n">auth</span> <span class="n">username</span><span class="o">=</span><span class="s1">&#39;libvirt&#39;</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">secret</span> <span class="nb">type</span><span class="o">=</span><span class="s1">&#39;ceph&#39;</span> <span class="n">uuid</span><span class="o">=</span><span class="s1">&#39;{uuid of secret}&#39;</span><span class="o">/&gt;</span>
<span class="o">&lt;/</span><span class="n">auth</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="n">target</span> <span class="o">...</span>
</pre></div>
</div>
<p><strong>注：</strong>示例 ID 是 <code class="docutils literal notranslate"><span class="pre">libvirt</span></code> ，
不是<a class="reference internal" href="#ceph">配置 Ceph</a> 生成的 Ceph 名 <code class="docutils literal notranslate"><span class="pre">client.libvirt</span></code> ，
确保你用的是 Ceph 名的 ID 部分。
如果出于某些原因你需要更换密钥，
必须先执行 <code class="docutils literal notranslate"><span class="pre">sudo</span> <span class="pre">virsh</span> <span class="pre">secret-undefine</span> <span class="pre">{uuid}</span></code> 、
然后再执行 <code class="docutils literal notranslate"><span class="pre">sudo</span> <span class="pre">virsh</span> <span class="pre">secret-set-value</span></code> 。</p>
</li>
</ol>
</section>
<section id="id3">
<h2>总结<a class="headerlink" href="#id3" title="Permalink to this heading"></a></h2>
<p>完成上面的配置后你就可以启动 VM 了，
为确认 VM 和 Ceph 在通讯，你可以执行如下过程。</p>
<ol class="arabic">
<li><p>检查 Ceph 是否在运行：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ceph</span> <span class="n">health</span>
</pre></div>
</div>
</li>
<li><p>检查 VM 是否在运行。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sudo</span> <span class="n">virsh</span> <span class="nb">list</span>
</pre></div>
</div>
</li>
<li><p>检查 VM 是否在和 Ceph 通讯，用你的 VM 域名字替换 <code class="docutils literal notranslate"><span class="pre">{vm-domain-name}</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sudo</span> <span class="n">virsh</span> <span class="n">qemu</span><span class="o">-</span><span class="n">monitor</span><span class="o">-</span><span class="n">command</span> <span class="o">--</span><span class="n">hmp</span> <span class="p">{</span><span class="n">vm</span><span class="o">-</span><span class="n">domain</span><span class="o">-</span><span class="n">name</span><span class="p">}</span> <span class="s1">&#39;info block&#39;</span>
</pre></div>
</div>
</li>
<li><p>检查一下 <code class="docutils literal notranslate"><span class="pre">&lt;target</span> <span class="pre">dev='vdb'</span> <span class="pre">bus='virtio'/&gt;</span></code> 里的设备是否存在：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">virsh</span> <span class="n">domblklist</span> <span class="p">{</span><span class="n">vm</span><span class="o">-</span><span class="n">domain</span><span class="o">-</span><span class="n">name</span><span class="p">}</span> <span class="o">--</span><span class="n">details</span>
</pre></div>
</div>
</li>
</ol>
<p>如果看起来一切正常，你就可以在虚拟机内使用
Ceph 块设备了。</p>
</section>
</section>



<div id="support-the-ceph-foundation" class="admonition note">
  <p class="first admonition-title">Brought to you by the Ceph Foundation</p>
  <p class="last">The Ceph Documentation is a community resource funded and hosted by the non-profit <a href="https://ceph.io/en/foundation/">Ceph Foundation</a>. If you would like to support this and our other efforts, please consider <a href="https://ceph.io/en/foundation/join/">joining now</a>.</p>
</div>


           </div>
           
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="../qemu-rbd/" class="btn btn-neutral float-left" title="QEMU 与块设备" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="../rbd-kubernetes/" class="btn btn-neutral float-right" title="Block Devices and Kubernetes" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2016, Ceph authors and contributors. Licensed under Creative Commons Attribution Share Alike 3.0 (CC-BY-SA-3.0).</p>
  </div>

   

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>